package com.frezrik.jetpackpractice.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.frezrik.jetpackpractice.R;
import com.frezrik.jetpackpractice.databinding.FragmentHomeBinding;
import com.frezrik.jetpackpractice.viewmodel.DataViewModel;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    private FragmentHomeBinding mBinding;

    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentHomeBinding.inflate(inflater);
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 注意多个activity共享ViewModel时，需要使用同一个ViewModelStoreOwner，这里用requireActivity()
        DataViewModel viewModel = new ViewModelProvider(requireActivity()).get(DataViewModel.class);

        mBinding.jumpBtn.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_homeFragment_to_userFragment));

        mBinding.setData(viewModel);
        mBinding.setLifecycleOwner(this);
    }
}